package me.zhengjie.modules.finace_taxation.util;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 图片上传
 * @Author: 杨荣森
 * @Date: 2020-12-23
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "图片上传")
@RestController
@RequestMapping("/api/ft/uploadUtil")
public class FileUpload {

    @Value("${file.windows.path}")
    private String UPLOADPATH;

    @Value("${file.windows.result_path}")
    private String RESULTPATH;

    /*
     * 上传图片
     * */
    @PostMapping("/uploadImage")
    public JSONObject uploadImage(@RequestParam(value = "file") MultipartFile[] files) {
        JSONObject result = new JSONObject();
        String uploadPath = this.UPLOADPATH;
        File uploadDirectory = new File(uploadPath);
        String fileName = null;
        String resultPath = null;
        if (uploadDirectory.exists()) {
            if (!uploadDirectory.isDirectory()) {
                uploadDirectory.delete();
            }
        } else {
            uploadDirectory.mkdir();
        }
        //这里支持多文件上传
        if (files != null) {
            BufferedOutputStream bufferedOutputStream = null;
            try {
                List<String> resultPaths = new ArrayList<>();
                for(MultipartFile file : files) {
                    Long uploadFileName = System.currentTimeMillis();
                    fileName = file.getOriginalFilename();
                    //判断文件是否为图片文件
                    if (fileName != null && !"".equalsIgnoreCase(fileName.trim()) && ImageFilesUtil.isImageFile(fileName)) {
                        File outFile = new File(uploadPath + "/" + uploadFileName + ImageFilesUtil.getFileType(fileName));
                        file.transferTo(outFile);
                        resultPaths.add(this.RESULTPATH + uploadFileName + ImageFilesUtil.getFileType(fileName));
                    }
                }
                //返回路径
                resultPath = StringUtils.join(resultPaths,",");
                result.put("message","上传成功");
                result.put("code","200");
                result.put("success",true);
                result.put("result",resultPath);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
                result.put("message","服务器响应失败");
                result.put("code","500");
                result.put("success",false);
                return result;
            } finally {
                try {
                    if (bufferedOutputStream != null) {
                        bufferedOutputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    result.put("message","服务器响应失败");
                    result.put("code","500");
                    result.put("success",false);
                    return result;
                }
            }
        } else {
            result.put("message","图片不能为空");
            result.put("code","500");
            result.put("success",false);
            return result;
        }
    }
}
